﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.FormInfo
{
    public partial class FormDefDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //设置对象
                int uid = 0;
                try
                {
                    uid = Int32.Parse(Request["uid"]);
                }
                catch
                {

                }
                SysFormDef item = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    item = session.Get<SysFormDef>(uid);
                    if (item != null)
                    {
                        //保存uid
                        ViewState["uid"] = uid;
                    }
                    else
                    {
                        item = new SysFormDef();
                        item.Status = "1";
                        item.StartDate = DateTime.Now;
                        item.EndDate = DateTime.Now.AddDays(15);
                        ViewState["kind"] = Request["kind"];
                    }
                }

                //设置控件值
                title.Text = item.Title;
                start.Text = string.Format("{0:yyyy-MM-dd}", item.StartDate);
                end.Text = string.Format("{0:yyyy-MM-dd}", item.EndDate);
                imageUrl.Value = item.ImageUrl;
                txtContent.Value = item.Content;
                summary.Text = item.Description;
                limitday.Text = item.LimitDay.ToString();
                limitcount.Text = item.LimitCount.ToString();
                url.Text = item.SubmitReturn;
                formStyle.Text = item.FormStyle;

                status.SelectedValue = item.Status;
            }
            else
            {

                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];
                SysFormDef item = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    item = session.Get<SysFormDef>(uid);
                    if (item == null)
                    {
                        item = new SysFormDef();
                        item.Status = "1";
                        item.Kind = ViewState["kind"] as string;
                        item.CreateDate = DateTime.Now;
                        item.OwnerAccount = UserKit.GetUserAccountFailLogin();
                    }

                    //在此设置item的各个属性
                    item.Title = title.Text;

                    item.StartDate = DateTime.Parse(start.Text);
                    item.EndDate = DateTime.Parse(end.Text);
                    item.LimitDay = Convert.ToInt32(limitday.Text);
                    item.LimitCount = Convert.ToInt32(limitcount.Text);
                    item.ImageUrl = imageUrl.Value;
                    item.Content = txtContent.Value;
                    item.Description = summary.Text;
                    item.Status = status.SelectedValue;
                    item.SubmitReturn = url.Text;
                    item.FormStyle = formStyle.Text;


                    //保存并更新item
                    session.SaveOrUpdate(item);
                    session.Flush();
                }


                //框架返回成功提示
                ZLCMS.Business.DwzFrame.FrameHelper _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
                ZLCMS.Business.DwzFrame.ReturnCode rc = new ZLCMS.Business.DwzFrame.ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/FormInfo/FormDefDetail.aspx?tabid=FormDefDetail";

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + item.Id;
                }

                _framePage.Alert(rc);
            }
        }
    }
}